package com.zx.service.fileupload.impl;

import com.zx.service.fileupload.FileUploadService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

/**
 * @author sunkai
 * @date 2022/8/27 0027 20:46
 * @mood zhou xun
 */
@Service
public class FileUploadServiceImpl implements FileUploadService {
    @Override
    public String upload(MultipartFile file, HttpServletRequest request) throws IOException {
        //上传路径
        String path="C:\\images";
        File realPath = new File(path);
        if (!realPath.exists()){
            realPath.mkdir();
        }
        //获取到上传文件的名称
        String filename = file.getOriginalFilename();
        //获取文件后缀
        String exName = StringUtils.getFilenameExtension(filename);
        String newFileName = UUID.randomUUID().toString()+ "." + exName;
        //上传文件
        file.transferTo(new File(path, newFileName));
        return newFileName;
    }
}
